package com.gong.chooselesson.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gong.chooselesson.mapper.LessonMapper;
import com.gong.chooselesson.pojo.Lesson;
import com.gong.chooselesson.service.LessonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

/**
 * @Description
 * @Author gongchang
 * @Date 2021-07-27 16:34
 */
@Service
public class LessonServiceImpl extends ServiceImpl<LessonMapper, Lesson> implements LessonService {

    @Autowired
    Jedis readJedis;
    @Autowired
    Jedis writeJedis;
    @Autowired
    LessonMapper lessonMapper;

    @Override
    public int getLessonCount(int lid) {
        String s = readJedis.get("lesson:"+lid + ":l_left");
        if (s==null || "null".equals(s)){
            Lesson lesson = lessonMapper.selectById(lid);
            System.out.println(lesson);
            writeJedis.set("lesson:"+lid + ":l_left",""+lesson.getLLeft());
            return lesson.getLLeft();
        }

        return Integer.valueOf(s);
    }

    @Override
    public int getLessonLnoByLid(int lid) {
        String s = readJedis.get("lesson:" + lid + ":l_no");
        if ("nil".equals(s)){
            Lesson lesson = lessonMapper.selectById(lid);
            writeJedis.set("lesson:"+lid+":l_no",lesson.getLNo()+"");
            return lesson.getLNo();
        }
        return Integer.valueOf(s);
    }
}
